package com.liziyi0914.morearmor.effect;

import com.google.common.collect.Lists;
import com.liziyi0914.morearmor.armor.BaseArmor;
import com.liziyi0914.morearmor.data.DataManager;
import com.liziyi0914.morearmor.event.IListener;
import lombok.Builder;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.boss.WitherEntity;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public abstract class AbstractEffect implements IListener {

    protected Random random = new Random();

    public abstract Predicate<LivingEntity> predicateArmor();

    protected ArmorDesc getArmorDesc(LivingEntity entity) {
        final ArmorDesc.ArmorDescBuilder builder = ArmorDesc.builder();
        for (BaseArmor baseArmor : DataManager.get(entity)) {
            if (baseArmor.getSlotType() == EquipmentSlot.HEAD) {
                builder.helmet(true);
            }
            if (baseArmor.getSlotType() == EquipmentSlot.CHEST) {
                builder.chestplate(true);
            }
            if (baseArmor.getSlotType() == EquipmentSlot.LEGS) {
                builder.leggings(true);
            }
            if (baseArmor.getSlotType() == EquipmentSlot.FEET) {
                builder.boots(true);
            }
        }
        return builder.build();
    }

    protected List<LivingEntity> getNearEntity(ServerWorld world, LivingEntity entity, double raids) {
        return getNearEntity(world, entity, raids, false);
    }

    //todo 无法获取附近实体。https://gitee.com/liziyi0914/MoreArmor/issues/I3YHE2
    protected List<LivingEntity> getNearEntity(ServerWorld world, LivingEntity entity, double raids, boolean self) {
        List<LivingEntity> entities = Lists.newArrayList();
        final BlockPos pos = entity.getBlockPos();
        entities.addAll(new ArrayList<>(world.getEntitiesByClass(LivingEntity.class, new Box(new BlockPos(pos.getX() - raids, pos.getY() - raids, pos.getY() - raids)
                , new BlockPos(pos.getX() + raids, pos.getY() + raids, pos.getY() + raids)), entity1 -> entity.getPos().distanceTo(entity1.getPos()) < raids)));
        if (self) {
            entities.add(entity);
        }
        return entities;
    }

    @Builder
    public static class ArmorDesc {
        boolean helmet;
        boolean chestplate;
        boolean leggings;
        boolean boots;
    }
}
